package cn.kxm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.kxm.pojo.InstroageBill;
import cn.kxm.pojo.InstroageBillDetail;
import cn.kxm.pojo.PartType;
import cn.kxm.service.CompanyService;
import cn.kxm.service.DictService;
import cn.kxm.service.InstroageBillDetailService;
import cn.kxm.service.InstroageBillService;
import cn.kxm.service.PartService;
import cn.kxm.service.PartTypeService;
import cn.kxm.service.PurchaseOrderBillDetailService;
import cn.kxm.service.PurchaseOrderBillService;
import cn.kxm.util.MakeNewCode;

@Controller
@RequestMapping("/instroageBill")
public class InstroageBillController extends BaseController {
	
	@Autowired
    private InstroageBillService instroageBillService;
	@Autowired
	private InstroageBillDetailService detailService;
	@Autowired
	private MakeNewCode makeNewCode;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private DictService dictService;
	@Autowired
	private PurchaseOrderBillService pOrderBillService;
	@Autowired
	private PurchaseOrderBillDetailService pDetailService;
	@Autowired
	private PartService partService;
	@Autowired
	private PartTypeService partTypeService;
	
	@RequestMapping("getInstroageBillList.json")
	@ResponseBody
	public Object getInstroageBills(@RequestParam(value="page",required=false)Integer page,
			                        @RequestParam(value="rows",required=false)Integer size,
			                        @RequestParam(value="findcode",required=false)String findcode,
			                        @RequestParam(value="findstartDate",required=false)Date findstartDate,
			                        @RequestParam(value="findendDate",required=false)Date findendDate,
			                        @RequestParam(value="findsupplierName",required=false)String findsupplierName
			                        ){
		System.out.println(page+">>>>>>>>>>>>>>>>>>>>>"+size);
		List<InstroageBill> bills=instroageBillService.getInstroageBillList(page, size, findcode, findstartDate, findendDate, findsupplierName);
		Map<String, Object> jsonMaps=new HashMap<String, Object>();
		Integer total=instroageBillService.findInstroageBillCount();
		int amounts=0;//数量总和
		double moneys=0;//总价总和
		for (InstroageBill instroageBill : bills) {
			amounts+=instroageBill.getBamount();
		}
		for (InstroageBill instroageBill : bills) {
			moneys+=instroageBill.getMoney();
		}
		jsonMaps.put("isFooter", true);
		jsonMaps.put("customName", "合计");
		jsonMaps.put("bamount", amounts);
		jsonMaps.put("money",moneys);
		JSONArray array=new JSONArray();
		JSONObject object=new JSONObject();
		object.putAll(jsonMaps);
		array.add(object);
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("footer", array);
		maps.put("total", total);
		maps.put("rows", bills);
		System.out.println(JSON.toJSONString(maps));
		return JSON.toJSONString(maps);
	}
	
	@RequestMapping("getBillDetailList.json")
	@ResponseBody
	public Object getBillDetailList(String billCode){
		List<InstroageBillDetail> billDetails=detailService.finDetailsByBillCode(billCode);
		Map<String, Object> jsonMaps=new HashMap<String, Object>();
		int amounts=0;//数量总和
		double moneys=0;//总价总和
		for (InstroageBillDetail detail : billDetails) {
			amounts+=detail.getAmount();
		}
		for (InstroageBillDetail detail : billDetails) {
			moneys=moneys+(detail.getPrice()*detail.getAmount());
		}
		jsonMaps.put("isFooter", true);
		jsonMaps.put("partName", "合计");
		jsonMaps.put("amount", amounts);
		jsonMaps.put("money", moneys);
		JSONArray array=new JSONArray();
		JSONObject object=new JSONObject();
		object.putAll(jsonMaps);
		array.add(object);
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("footer", array);
		maps.put("rows", billDetails);
		maps.put("total", billDetails.size());
		return JSON.toJSONString(maps);
	}
	
	@RequestMapping("createInstroageBillCode")
	@ResponseBody
	public Object createCode(){
		return JSON.toJSONString(makeNewCode.getRandomCodeByPrefix("RK"));
	}
	
	@RequestMapping("getCompanyList.json")
	@ResponseBody
	public Object getCompanyList(@RequestParam(value="page",required=false)Integer page,
            @RequestParam(value="rows",required=false)Integer size,
            @RequestParam(value="code",required=false)String code,
            @RequestParam(value="name",required=false)String name,
            @RequestParam(value="type",required=false)Integer type
            ){
		Map<String, Object>maps=new HashMap<String, Object>();
		List<Integer> types=new ArrayList<Integer>();
		if(type==null){
			types.add(2);
		}else{
			types.add(type);
		}
		maps.put("total", companyService.getCompanyCount(types, name, code));
		maps.put("rows", companyService.getCompanys(types, name, code, page, size));
		return JSON.toJSONString(maps);
	}
	
	@RequestMapping("getInstroageType")
	@ResponseBody
	public Object getInstroageType(String type){
		return JSON.toJSONString(dictService.getDictListByType(type));
	}
	
	@RequestMapping("getSupplierOrderList.json")
	@ResponseBody
	public Object getSupplierOrderList(@RequestParam(value="page",required=false)Integer page,
            @RequestParam(value="rows",required=false)Integer size,
            @RequestParam(value="code",required=false)String code,
            @RequestParam(value="customName",required=false)String companyName,
            @RequestParam(value="fromDate",required=false)Date fromDate,
            @RequestParam(value="endDate",required=false)Date endDate){
		System.out.println(companyName);
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("total", pOrderBillService.getPurchaseOrderBillByCompanyNameCount(companyName, fromDate, endDate, code));
		maps.put("rows", pOrderBillService.getPurchaseOrderBillByCompanyName(companyName, page, size, fromDate, endDate, code));
		return JSON.toJSONString(maps);
	}
	@RequestMapping("getPartByCode.json")
	@ResponseBody
	public Object getPartByCode(@RequestParam(value="billCode[]",required=false) String[] billCode,
			                    @RequestParam(value="codes[]",required=false)String[] codes){
		Map<String, Object> jsonMaps=new HashMap<String, Object>();
		int amounts=0;//数量总和
		double moneys=0;//总价总和
		if(billCode==null&&codes==null){
			//String nullDate="{\"rows\":[],\"total\":0;\"footer\":[]}";
			return null;
		}else{
			List<Map<String, Object>> partList=null;
			if(billCode!=null&&codes!=null&&codes.length!=0){
				partList=pDetailService.getPurchaseOrderBillDetailListByBillCode(billCode);
				List<Map<String, Object>> partList2=partService.getPartListByCodes(codes);
				partList.addAll(partList2);
			}else if(billCode!=null&&codes==null){
				partList=pDetailService.getPurchaseOrderBillDetailListByBillCode(billCode);
			}else if(billCode==null&&codes!=null&&codes.length!=0){
				partList=partService.getPartListByCodes(codes);
			}
			for (Map<String, Object> part : partList) {
				amounts=((Integer) (part.get("amount")==null?1:part.get("amount")))+amounts;
			}
			Double price1=0.00;
			
			for (Map<String, Object> part : partList) {
				if(part.get("price")!=null){
					float price2=(Float) part.get("price");
					price1=(double) price2;
				}
				moneys=moneys+(((Integer) (part.get("amount")==null?1:part.get("amount")))*price1);
			}
			jsonMaps.put("isFooter", true);
			jsonMaps.put("partBrand", "合计");
			jsonMaps.put("amount", amounts);
			jsonMaps.put("money", moneys);
			JSONArray array=new JSONArray();
			JSONObject object=new JSONObject();
			object.putAll(jsonMaps);
			array.add(object);
			Map<String, Object> maps=new HashMap<String, Object>();
			maps.put("footer", array);
			maps.put("rows", partList);
			maps.put("total", partList.size());
			System.out.println(JSON.toJSONString(maps));
			return JSON.toJSONString(maps);
		}
	}
	@RequestMapping("getPartList.json")
	@ResponseBody
	public Object getPartList(@RequestParam(value="customName")String customName,
			@RequestParam(value="page")Integer page,
			@RequestParam(value="rows")Integer rows,
			@RequestParam(value="size",required=false)String size,
			@RequestParam(value="partNo",required=false)String partNo,
			@RequestParam(value="name",required=false)String name,
			@RequestParam(value="type",required=false)Integer type){
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("total", partService.getPartListCount(size, partNo, name, type));
		maps.put("rows", partService.getPartList(customName, page, rows, size, partNo, name, type));
		return JSON.toJSONString(maps);
	}
	@RequestMapping("getPartTypeList.json")
	@ResponseBody
	public Object getPartTypeList(){
		PartType partType=new PartType();
		partType.setId(0);
		partType.setTypeName("--请选择--");
		List<PartType> lists=partTypeService.getPartTypeList();
		lists.add(0, partType);
		return lists;
	}
	
	@RequestMapping("saveInstroageBill.json")
	@ResponseBody
	public Object saveInstroageBill(@RequestParam(value="total")Double total,
			                        @RequestParam(value="codes")String codes,
			                        @RequestParam(value="inBill",required=false)String inBill){
		Map<String, Object>maps=new HashMap<String, Object>();
		if(instroageBillService.addInstroageBill(inBill, codes,total)){
			maps.put("result", "success");	
		}else{
			maps.put("result", "error");	
		}
		return JSON.toJSONString(maps);
	}
	
	@RequestMapping("modifyInstroageBill.html")
	public String modifyObjectPage(String code,HttpServletRequest request){
		InstroageBill inBill=instroageBillService.getInstroageBillByCode(code);
		request.setAttribute("inBill", inBill);
		return "web/goods/modifyInstroageBill";
	}
	@RequestMapping("getPartByInstroageCode.json")
	@ResponseBody
	public Object getPartByInstroageCode(@RequestParam(value="billCode[]",required=false) String[] billCode,
			@RequestParam(value="codes[]",required=false)String[] codes){
		Map<String, Object> jsonMaps=new HashMap<String, Object>();
		int amounts=0;//数量总和
		double moneys=0;//总价总和
		if(billCode==null&&codes==null){
			//String nullDate="{\"rows\":[],\"total\":0;\"footer\":[]}";
			return null;
		}else{
			List<Map<String, Object>> partList=null;
			List<Map<String, Object>> partList2=null;
			if(billCode!=null&&codes!=null&&codes.length!=0){
				if(billCode[0].substring(0, 2).equalsIgnoreCase("RK")){
					partList=detailService.findDetailByCode(billCode[0]);
					partList2=partService.getPartListByCodes(codes);
				}else{
					partList=pDetailService.getPurchaseOrderBillDetailListByBillCode(billCode);
					partList2=partService.getPartListByCodes(codes);
				}
				partList.addAll(partList2);
			}else if(billCode!=null&&codes==null){
				if(billCode[0].substring(0, 2).equalsIgnoreCase("RK")){
					partList=detailService.findDetailByCode(billCode[0]);
				}else{
					partList=pDetailService.getPurchaseOrderBillDetailListByBillCode(billCode);
				}
			}else if(billCode==null&&codes!=null&&codes.length!=0){
				partList=partService.getPartListByCodes(codes);
			}
			for (Map<String, Object> part : partList) {
				amounts=((Integer) (part.get("amount")==null?1:part.get("amount")))+amounts;
			}
			Double price1=0.00;
			
			for (Map<String, Object> part : partList) {
				if(part.get("price")!=null){
					float price2=(Float) part.get("price");
					price1=(double) price2;
				}
				moneys=moneys+(((Integer) (part.get("amount")==null?1:part.get("amount")))*price1);
			}
			jsonMaps.put("isFooter", true);
			jsonMaps.put("partBrand", "合计");
			jsonMaps.put("amount", amounts);
			jsonMaps.put("money", moneys);
			JSONArray array=new JSONArray();
			JSONObject object=new JSONObject();
			object.putAll(jsonMaps);
			array.add(object);
			Map<String, Object> maps=new HashMap<String, Object>();
			maps.put("footer", array);
			maps.put("rows", partList);
			maps.put("total", partList.size());
			System.out.println(JSON.toJSONString(maps));
			return JSON.toJSONString(maps);
		}
	}
	@RequestMapping("updateInstroageBill.json")
	@ResponseBody
	public Object updateInstroageBill(@RequestParam(value="total")Double total,
            @RequestParam(value="codes")String codes,
            @RequestParam(value="inBill",required=false)String inBill){
		Map<String, Object>maps=new HashMap<String, Object>();
		if(instroageBillService.modifyInstroageBill(inBill, codes, total)){
			maps.put("result", "success");	
		}else{
			maps.put("result", "error");	
		}
		return JSON.toJSONString(maps);
	}
	@RequestMapping("delrow.json")
	@ResponseBody
	public Object delRow(String code){
		Map<String, Object> maps=new HashMap<String, Object>();
		if(instroageBillService.delInstroageBillCode(code)){
			maps.put("result", "success");
		}else{
			maps.put("result", "error");
		}
		return JSON.toJSONString(maps);
	}
	@RequestMapping("delrows.json")
	@ResponseBody
	public Object delRows(@RequestParam("codes[]")String[] codes){
		System.out.println(codes[0]);
		Map<String, Object> maps=new HashMap<String, Object>();
		if(instroageBillService.delInstroageBillByArray(codes)){
			maps.put("result", "success");
		}else{
			maps.put("result", "error");
		}
		return JSON.toJSONString(maps);
	}
}
